// pages/shopManager/shopOrder/orderDetail/orderDetail.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userId: null,
        orderId: null,
        orderInfo: {},
        ewmCode: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            orderId: options.orderId,
            userId: wx.getStorageSync('userId')
        })
        this.getOrderInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    getOrderInfo: function () {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            order_id: this.data.orderId,
            get_user: 1,
            paramter: 1
        }
        common.post('Orderlist/user_order', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                orderInfo: res[0]
            })
            that.getOrderCode()
        }).catch(function (e) { })
    },

    getOrderCode: function () {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            orderId: this.data.orderId,
            img: this.data.orderInfo.goods[0].sku_info.img
        }
        common.post('Qrcodes/create_project', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                ewmCode: res
            })
        }).catch(function (e) { })
    },

    confirmOrder: function (e) {
        const that = this
        wx.showModal({
            title: '提示',
            content: '是否确认订单？',
            success(res) {
                if (res.confirm) {
                    let data = {
                        order_id: e.currentTarget.dataset.id
                    }
                    common.post('Orderlist/store_affirm_order', data).then(function (res) {
                        if (res.code == 204) {
                            wx.showToast({
                                title: res.error,
                                icon: 'none',
                                duration: 2000
                            })
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '成功确认订单',
                                showCancel: false,
                                success(res) {
                                    if (res.confirm) {
                                        that.getOrderInfo()
                                    }
                                }
                            })
                        }
                    }).catch(function (e) { })
                }
            }
        })
    }
})